package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysBaseParam;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.mapper.system.ISysBaseParamMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysBaseParamService;
import com.wejoy.video.middle.station.data.constant.RedisKeys;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统--基础参数
 *
 * @author Administrator
 * @date 2021年10月26日 16:36:52
 */
@Service
public class SysBaseParamServiceImpl implements ISysBaseParamService {
    @Autowired
    private ISysBaseParamMapper sysBaseParamMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    @Lazy
    private ISysBaseParamService sysBaseParamService;

    @Override
    public void save(SysBaseParam req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysBaseParam req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        if (null != getOneByUniCode(req.getUniCode())) {
            throw new BusinessException("已存在同名参数值");
        }
        checkStringLength(req, false);
        return sysBaseParamMapper.insertSelective(req);
    }

    @Override
    @CacheEvict(value = RedisKeys.SYS_BASE_PARAM_KEY, key = "#sysBaseParam.uniCode")
    public int updateSelectiveById(SysBaseParam sysBaseParam) {
        WJAssert.notNull(sysBaseParam, "入参对象不能为空");
        WJAssert.notNull(sysBaseParam.getId(), "id参数错误");
        SysBaseParam where = new SysBaseParam();
        where.setId(sysBaseParam.getId());
        return sysBaseParamMapper.updateSelective(sysBaseParam, where);
    }

    @Override
    public int getCount(SysBaseParam sysBaseParam) {
        checkStringLength(sysBaseParam, true);
        return sysBaseParamMapper.getCount(sysBaseParam);
    }

    @Override
    public SysBaseParam getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysBaseParam where = new SysBaseParam();
        where.setId(id);
        return sysBaseParamMapper.getOne(where);
    }

    @Override
    public List<SysBaseParam> getList(SysBaseParam sysBaseParam) {
        checkStringLength(sysBaseParam, true);
        return sysBaseParamMapper.getList(sysBaseParam);
    }

    @Override
    public List<SysBaseParam> getPageList(SysBaseParam sysBaseParam) {
        checkStringLength(sysBaseParam, true);
        return sysBaseParamMapper.getPageList(sysBaseParam);
    }

    @Override
    public void audit(Long id, String operator) {
        SysBaseParam req = new SysBaseParam();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());

        SysBaseParam baseParamDB = getOneById(id);
        req.setUniCode(baseParamDB.getUniCode());
        sysBaseParamService.updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysBaseParam req = new SysBaseParam();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());

        SysBaseParam baseParamDB = getOneById(id);
        req.setUniCode(baseParamDB.getUniCode());
        sysBaseParamService.updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysBaseParam req = new SysBaseParam();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Cacheable(value = RedisKeys.SYS_BASE_PARAM_KEY, key = "#uniCode")
    @Override
    public SysBaseParam getOneByUniCode(String uniCode) {
        WJAssert.isMaxLength(uniCode, 50, "UNICODE非法");
        SysBaseParam where = new SysBaseParam();
        where.setUniCode(uniCode);
        where.setStatus(EnumStatus.AUDIT.getCode());
        return sysBaseParamMapper.getOne(where);
    }


    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysBaseParam sysBaseParam, boolean nullAble) {
        WJAssert.notNull(sysBaseParam, "入参对象不能为空");
        WJAssert.limitMaxLength(sysBaseParam.getUniCode(), nullAble, 50, "唯一编码不合法!");
        WJAssert.limitMaxLength(sysBaseParam.getValue(), nullAble, 2000, "值不合法!");
        WJAssert.limitMaxLength(sysBaseParam.getDescription(), nullAble, 300, "描述不合法!");
        WJAssert.limitMaxLength(sysBaseParam.getOperator(), nullAble, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysBaseParam.getRemark(), true, 255, "备注不合法!");
    }
}
